package com.zahb.qadx.net;

import android.text.TextUtils;

import com.google.gson.GsonBuilder;
import com.zahb.qadx.net.interceptor.LoggerInterceptor;

import java.util.concurrent.TimeUnit;

import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitStudyTimeService {

    public static String sCollectionType = "";
    public static final String sCollectionTypeVideo = "video";
    public static final String sCollectionTypePractice = "practice";
    private static final String BASE_URL = "http://121.196.44.61:8385/";
    static final String BASE_URL_DIR = "";
    private static ApiService mApiService;
    private static final long DEFAULT_CONNECT_TIME_OUT = 10L;
    private static final long DEFAULT_WRITE_TIME_OUT = 10L;
    private static final long DEFAULT_READ_TIME_OUT = 10L;

    public static ApiService getNetService() {
        if (mApiService == null) {
            synchronized (RetrofitStudyTimeService.class) {
                if (mApiService == null) {
                    OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                            .retryOnConnectionFailure(true)
                            .addInterceptor(sCommonInterceptor)
                            .addNetworkInterceptor(new LoggerInterceptor(LoggerInterceptor.TAG, true))
                            .connectTimeout(DEFAULT_CONNECT_TIME_OUT, TimeUnit.SECONDS)
                            .writeTimeout(DEFAULT_WRITE_TIME_OUT, TimeUnit.SECONDS)
                            .readTimeout(DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS)
                            .build();

                    GsonBuilder gsonBuilder = new GsonBuilder();
                    gsonBuilder.registerTypeAdapterFactory(new CustomTypeAdapterFactory());
                    Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(BASE_URL)
                            .client(okHttpClient)
                            .addConverterFactory(GsonConverterFactory.create(gsonBuilder.create()))
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                            .build();

                    mApiService = retrofit.create(ApiService.class);
                }
            }
        }

        return mApiService;
    }

    /**
     * 封装公共参数
     */
    private static final Interceptor sCommonInterceptor = chain -> {
        Request oldRequest = chain.request();

        String platform = "";
        if (TextUtils.equals(sCollectionType, sCollectionTypeVideo)) {
            // header添加platform，1(测试环境学习时长)，2(生产环境学习时长)，3(预生产环境学习时长)
            if (Integer.valueOf(RetrofitService.TYPE_ENVIRONMENT) == RetrofitService.TYPE_ENVIRONMENT_DEBUG)
                platform = "1";
            else if (Integer.valueOf(RetrofitService.TYPE_ENVIRONMENT) == RetrofitService.TYPE_ENVIRONMENT_RELEASE)
                platform = "2";
            else if (Integer.valueOf(RetrofitService.TYPE_ENVIRONMENT) == RetrofitService.TYPE_ENVIRONMENT_DEV)
                platform = "3";
        } else if (TextUtils.equals(sCollectionType, sCollectionTypePractice)) {
            // header添加platform，7(测试环境学习时长)，8(生产环境学习时长)，9(预生产环境学习时长)
            if (Integer.valueOf(RetrofitService.TYPE_ENVIRONMENT) == RetrofitService.TYPE_ENVIRONMENT_DEBUG)
                platform = "7";
            else if (Integer.valueOf(RetrofitService.TYPE_ENVIRONMENT) == RetrofitService.TYPE_ENVIRONMENT_RELEASE)
                platform = "8";
            else if (Integer.valueOf(RetrofitService.TYPE_ENVIRONMENT) == RetrofitService.TYPE_ENVIRONMENT_DEV)
                platform = "9";
        }

        Request.Builder requestBuilder = oldRequest.newBuilder();
        if (!TextUtils.isEmpty(platform))
            requestBuilder.addHeader("platform", platform);

        return chain.proceed(requestBuilder.build());
    };
}
